using System;
using System.Runtime.Serialization;
using Habanero.Base.Exceptions;

namespace FireStarter.CodeGeneration
{
    public class PropertyMissingException : HabaneroApplicationException
    {
        private readonly string _propertyName;

        public PropertyMissingException(string propertyName): base( GetMessage(null, propertyName))
        {
            _propertyName = propertyName;
        }

        public PropertyMissingException(string propertyName, string message)
            : base(GetMessage(message, propertyName))
        {
            _propertyName = propertyName;
        }

        public PropertyMissingException(string propertyName, string message, Exception innerException)
            : base(GetMessage(message, propertyName), innerException)
        {
            _propertyName = propertyName;
        }

        private static string GetMessage(string message, string propertyName)
        {
            return message ?? string.Format("Property not set: '{0}'", propertyName);
        }

        protected PropertyMissingException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            _propertyName = info.GetString("PropertyName");
        }

        public string PropertyName
        {
            get { return _propertyName; }
        }

        public override string ToString()
        {
            return string.Format("Property not set: '{0}'", PropertyName);
        }
    }
}